package org.rcloud.io.service.impl;

import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.mapper.ArticleCategoryMapper;
import org.rcloud.io.model.ArticleCategoryWithBLOBs;
import org.rcloud.io.service.ArticleCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleCategoryServiceImpl implements ArticleCategoryService {

    @Autowired
    private ArticleCategoryMapper acm;

    @Override
    public PageVo<ArticleCategoryWithBLOBs> findByTitle(String title) {
        try {
            List<ArticleCategoryWithBLOBs> list = acm.findByTitle(title);
            return PageVo.setOk(list);
        } catch (Exception e) {
            e.printStackTrace();
            return PageVo.setError(e.getMessage());
        }
    }

    @Override
    public ResultVo addGame(ArticleCategoryWithBLOBs articleCategory) {
        if (articleCategory.equals("") || articleCategory == null) {
            return ResultVo.setError("传入数据为空");
        } else {
            try {
                int i = acm.insertSelective(articleCategory);
                if (i > 0) {
                    return ResultVo.setOK(i);
                } else {
                    return ResultVo.setError("添加失败");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return ResultVo.setError(e.getMessage());
            }
        }
    }

    @Override
    public ResultVo update(ArticleCategoryWithBLOBs articleCategory) {
        if (articleCategory.equals("") || articleCategory == null) {
            return ResultVo.setError("传入数据为空");
        } else {
            try {
                int i = acm.updateByPrimaryKeySelective(articleCategory);
                if (i > 0) {
                    return ResultVo.setOK(i);
                } else {
                    return ResultVo.setError("修改失败");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return ResultVo.setError(e.getMessage());
            }
        }
    }

    @Override
    public ResultVo findById(Integer id) {
        ArticleCategoryWithBLOBs articleCategory = acm.selectByPrimaryKey(id);
        return ResultVo.setOK(articleCategory);
    }

    @Override
    public PageVo<ArticleCategoryWithBLOBs> selectByPid(Integer pid) {
        try {
            List<ArticleCategoryWithBLOBs> list = acm.selectByPid(pid);
            if (list != null && !list.equals("")) {
                return PageVo.setOk(list);
            } else {
                return PageVo.setError("查询数据为空");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return PageVo.setError(e.getMessage());
        }
    }
}
